﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class OptionDa
    {
        public static void InsertOption(Option option)
        {
            InsertOption(option, DBHelper.GetDatabase(), null);
        }
        public static void InsertOption(Option option, TranscationHelper trans)
        {
            InsertOption(option, trans.DataBase, trans.Transaction);
        }
        private static void InsertOption(Option option, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                option.QuestionId,
                option.Value,
                option.Name,
                option.Sort
            };

            try
            {
                if (trans == null)
                    option.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertOption", parameters));
                else
                    option.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertOption", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void Delete(int id)
        {
            Delete(id, DBHelper.GetDatabase(), null);
        }
        public static void Delete(int id, TranscationHelper trans)
        {
            Delete(id, trans.DataBase, trans.Transaction);
        }
        private static void Delete(int id, Database db, DbTransaction trans)
        {
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_DeleteOption", id);
                else
                    db.ExecuteNonQuery(trans, "Usp_DeleteOption", id);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateOption(Option option)
        {
            UpdateOption(option, DBHelper.GetDatabase(), null);
        }
        public static void UpdateOption(Option option, TranscationHelper trans)
        {
            UpdateOption(option, trans.DataBase, trans.Transaction);
        }
        private static void UpdateOption(Option option, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                option.Id,
                option.QuestionId,
                option.Value,
                option.Name,
                option.Sort
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateOption", parameters);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateOption", parameters);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }

        }

        public static Option GetOption(int id)
        {
            Database db = DBHelper.GetDatabase();
            Option option = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetOption", id))
            {
                if (dataReader.Read())
                {
                    option = PopulateOption(dataReader);
                }
            }
            return option;
        }

        public static List<Option> GetOptionList(int quesId)
        {
            return GetOptionList(quesId, DBHelper.GetDatabase(), null);
        }
        public static List<Option> GetOptionList(int quesId, TranscationHelper trans)
        {
            return GetOptionList(quesId, trans.DataBase, trans.Transaction);
        }
        private static List<Option> GetOptionList(int quesId, Database db, DbTransaction trans)
        {
            List<Option> options = new List<Option>();
            try
            {
                IDataReader dataReader;
                if (trans == null)
                    dataReader = db.ExecuteReader("Usp_GetOptionList", quesId);
                else
                    dataReader = db.ExecuteReader(trans, "Usp_GetOptionList", quesId);

                using (dataReader)
                {
                    while (dataReader.Read())
                    {
                        options.Add(PopulateOption(dataReader));
                    }
                }
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }

            return options;
        }

        private static Option PopulateOption(IDataReader dataReader)
        {
            Option option = new Option();
            option.Id = dataReader.GetInt32(0);
            option.QuestionId = dataReader.GetInt32(1);
            option.Value = dataReader.GetString(2);
            option.Name = dataReader.GetString(3);
            option.Sort = dataReader.GetInt32(4);

            return option;
        }
    }
}
